﻿#include "login_widget.h"
#include "ui_login_widget.h"

login_widget::login_widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::login_widget),
    rwp(new register_widget),
    flp(new FaceLogin),
    Mainfucp(new MainFunction),
    boxFalse(this),
    boxTrue(this)
{
    ui->setupUi(this);

    //全部clear
    ui->lineEditIDCard->setClearButtonEnabled(true);

//    //事件过滤器
//    this->installEventFilter(this);

    //隐藏标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);

    //消息框
    boxFalse.setWindowTitle("提示");
    boxFalse.setText("未找到身份证号！请重新输入");
    boxFalse.addButton("确定",QMessageBox ::AcceptRole);

    boxTrue.setWindowTitle("提示");
    boxTrue.setText("身份证登录成功！请进入人脸验证");
    boxTrue.addButton("进入",QMessageBox ::AcceptRole);

    ConnectSlots();
}

//bool login_widget::eventFilter(QObject *watched, QEvent *event)
//{
//    //点击非输入框失去焦点
//    if(event->type() == QEvent::MouseButtonPress && watched !=  ui->lineEditIDCard)
//    {
//        ui->lineEditIDCard->setFocus();
//        ui->lineEditIDCard->clearFocus();
//        this->setFocus();
//        return true;
//    }
//    return QObject::eventFilter(watched,event);
//}

login_widget::~login_widget()
{
    delete ui;
}

void login_widget::ConnectSlots()
{
    //密码登录按钮
    connect(ui->pushButtonLogin,
            SIGNAL(clicked()), this, SLOT(pushButtonLogin_clicked()));

    //回车登录
    connect(ui->lineEditIDCard,
            SIGNAL(returnPressed()), this, SLOT(pushButtonLogin_clicked()));

    //注册按钮
    connect(ui->pushButtonRegister,
            SIGNAL(clicked()), this, SLOT(pushButtonRegister_clicked()));

}

void login_widget::pushButtonRegister_clicked()
{
    emit LogintoRegister();
}

void login_widget::pushButtonLogin_clicked()
{
    QString userId = ui->lineEditIDCard->text();
    //userId->remove(QRegExp("\\s")); QRegExp can not use
    QMap<QString,QString> userInfo = allTools::getDbUserInfo(userId); // get user_t info
    if(userId.size() != 18 || userInfo.empty())
    {
        if(boxFalse.exec())
        {
            //Nothing

        }

    }
    else
    {
        if(boxTrue.exec() == QMessageBox::AcceptRole)
        {
            emit LoginToFaceLogin(userInfo); //信号传递给控制界面
        }
    }

}

//隐藏标题框移动
void login_widget::mousePressEvent(QMouseEvent *e)
{
    last=e->globalPosition().toPoint();
}

void login_widget::mouseMoveEvent(QMouseEvent *e)
{
    int dx = e->globalPosition().toPoint().x() - last.x();
    int dy = e->globalPosition().toPoint().y() - last.y();
    last = e->globalPosition().toPoint();
    move(x()+dx, y()+dy);
}

void login_widget::mouseReleaseEvent(QMouseEvent *e)
{
    int dx = e->globalPosition().toPoint().x() - last.x();
    int dy = e->globalPosition().toPoint().y() - last.y();
    move(x()+dx, y()+dy);
}

//最小化
void login_widget::on_pushButtonMin_clicked()
{
    this->showMinimized();
}

//close
void login_widget::on_pushButtonQuit_clicked()
{
    emit closeAll();
}

